//
//  FunVC.m
//  爱寻找
//
//  Created by qianfeng0 on 16/6/23.
//  Copyright © 2016年 王超. All rights reserved.
//

#import "FunVC.h"
#import <AFNetworking.h>
#import <MJRefresh.h>
#import "Helper.h"
#import "FunModel.h"
#import "FunCell.h"
#import "WebVC.h"

@interface FunVC ()<UITableViewDataSource,UITableViewDelegate>
{
    int _page;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray * dataArray;
@end

@implementation FunVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    [self initUI];
    [self getData:(_page)];
    [self addRefresh];
    
}

#pragma mark - 懒加载
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
#pragma mark - 下拉刷新上拉加载更多
- (void)addRefresh {
    
    //添加下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];
    //添加上拉加载更多
    self.tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
}
- (void)getNewData {
    
    _page = 1;
    [self getData:_page];
    
}
- (void)getMoreData {
    
    _page++;
    [self getData:_page];
    
}

#pragma mark - 初始化UI
- (void)initUI {
    
    _page = 1;
    
    self.navigationController.tabBarItem.selectedImage = [[UIImage imageNamed:@"iconfont-qunawanhuodong-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor greenColor];
    
    //注册cell
    UINib * nib = [UINib nibWithNibName:@"FunCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"FunCell"];
    
}
#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //1.
    static NSString * identifier = @"FunCell";
    //2.
    FunCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FunModel * model = self.dataArray[indexPath.row];
    WebVC * vc = [[WebVC alloc] initWithNibName:@"WebVC" bundle:nil];
    
    vc.str = model.eventDetailUrl;
    vc.model = model;
    
    //隐藏分栏
    vc.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //实现给cell添加一个3D动画，
    //设置x,y的初始值为0.1，z的初始值为1
    //一般的动画效果都是在layer层实现的
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    [UIView animateWithDuration:0.5 animations:^{
        //设置cell 的x和y的最终值为1
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    }];
}
#pragma mark - 获取数据
- (void)getData:(int)page {
    
    AFHTTPSessionManager * mgr = [AFHTTPSessionManager manager];
    
    NSString * url = [NSString stringWithFormat:FUNURL,_page];
    
    [mgr GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (_page == 1) {
            [self.dataArray removeAllObjects];
        }
        //数组转模型
        NSArray * array = [FunModel arrayOfModelsFromDictionaries:responseObject error:nil];
        
        [self.dataArray addObjectsFromArray:array];
        [_tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}

@end











